package com.ssww.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.ssww.entity.Banner;
import com.ssww.service.BannerService;


@Controller
@RequestMapping("/job")
public class JobController implements SswwUrlController {
	@Resource
	private BannerService bannerService;
	
	@RequestMapping(value="tt")
	public String ssww(HttpServletRequest request, ModelMap modelMap) {
		 Banner  banner=new Banner();
		 banner.setHfDetail("cvsker");
		 banner.setHfLink("http://www.aicai.com");
		 banner.setHfName("new style");
		 banner.setHfPicUrl("dsda");
		 bannerService.addBanner(banner);
		return MAIN;
	}

	@RequestMapping(value="t/{url}")
	public String login(HttpServletRequest request, ModelMap modelMap,@PathVariable("url") String url) {
		System.out.println(url);

		return url;
	}
	@RequestMapping(value = "upload")   
	 public String handleFormUpload(MultipartHttpServletRequest request){     
	  String b = request.getParameter("a");   
	  System.out.println(b+"aaaaaaaaaaaaaaaaaaa");   
	  List<MultipartFile> file = request.getFiles("file");   
	  String path = request.getRealPath("/tmp/"); // 获取本地存储路径   
	  System.out.println(path + "-----------------");   
	  System.out.println(file.size()+"=====================");   
	  FileOutputStream fileOutputStream = null;   
	  for (int i = 0; i < file.size(); i++) {   
	   if (!file.get(i).isEmpty()) {   
	    String fileName =  file.get(i).getOriginalFilename();   
	    String a = fileName.split("\\.")[1];   
	    java.io.File files = new java.io.File(path + new Date().getTime() +i+ "."+a); // 新建一个文件   
	    try {   
	     fileOutputStream = new FileOutputStream(files);   
	     fileOutputStream.write(file.get(i).getBytes());   
	     System.out.println(file.get(i).getOriginalFilename()+ "-----------------");   
	     System.out.println(file.get(i).getContentType()+ "-----------------");   
	     fileOutputStream.flush();   
	    } catch (Exception e) {   
	     e.printStackTrace();   
	    }   
	    if (fileOutputStream != null) { // 关闭流   
	     try {   
	      fileOutputStream.close();   
	     } catch (IOException ie) {   
	      ie.printStackTrace();   
	     }   
	    }   
	   }   
	  }   
	   return MAIN;   
	 }  

	
}
